//
//  MNDBaofuBankListViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/6/8.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDBaofuBankListViewController.h"
#import "SYRefreshControl.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImage+JKResize.h"
#import "MNDBaofuBankListCell.h"

@interface MNDBaofuBankListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *banklist;

@end

@implementation MNDBaofuBankListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.isQuota) {
        self.tableView.allowsSelection = NO;
    }
    
    self.banklist = [NSMutableArray array];
    
//    WeakSelf(self);
    
//    [SYRefreshControl configGifMJRefresh:self.tableView refreshType:MJRefreshSupportRefresh beginRefresh:NO timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
//        
//        [weakself loadBankList];
//    } loadMoreBlock:^{
//        
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadBankList];
}

#pragma mark - Network

- (void)loadBankList {
    
    WeakSelf(self);
    [self.view beginLoading];
    
    [APIManager sendRequestWithPath:API_BAOFU_BANK_LIST parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            
            NSArray *array = [MNDBaofuBankListModel mj_objectArrayWithKeyValuesArray:responseModel.data[@"bank"]];
            
            [weakself.view configBlankPage:SYBlankPageTypeNoData hasData:array.count reloadButtonBlock:nil];
            
            [weakself.banklist removeAllObjects];
            [weakself.banklist addObjectsFromArray:array];
            [weakself.tableView reloadData];
            
        } else {
            [weakself.view configBlankPage:SYBlankPageTypeError hasData:NO reloadButtonBlock:^(id sender) {
                [weakself loadBankList];
            }];
        }
    } onFailure:^(NSError *error) {
        SYBlankPageType blankType = [weakself.view blankPageTypeWithError: error];
        [weakself.view configBlankPage:blankType hasData:NO reloadButtonBlock:^(id sender) {
            [weakself loadBankList];
        }];
    }];
}

#pragma mark - UITableView Datasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.banklist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cell";
    
    MNDBaofuBankListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    
    MNDBaofuBankListModel *bank = self.banklist[indexPath.row];
    cell.bankListModel = bank;
    
    return cell;
}

#pragma mark - UItableView Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MNDBaofuBankListModel *bankModel = self.banklist[indexPath.row];
    if (self.bankSelectedBlock) {
        self.bankSelectedBlock(bankModel);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
